import send_msg from '@feidao-factory/send-msg';

const DBACC_FIND = 'dbacc.find.intranet';

// 查询数据
/**
 * 查询数据
 * @param action_id 系统参数action_id
 * @param session_id 系统参数session_id
 * @param spaceid 系统参数spaceid
 * @param systemid 当前项目systemid
 * @param query_mode 查询的模块
 * @param tablename 表名
 * @param fields 字段名
 * @param criteria 条件
 * @param page_no 页码
 * @param page_size 条数
 * @param sort 排序
 * @param sql sql
 * @example dbacc_find(action_id, session_id, spaceid, systemid, query_mode, tablename, fields, criteria, page_no, page_size, sort, sql);
 */
export default async function dbacc_find(action_id: string, session_id: string, spaceid: string, systemid: string, query_mode: number, tablename: string, fields: any, criteria: any, page_no: number, page_size: number, sort: any, sql: string) {
	const msg = [{
		criteria,
		fields,
		page_no,
		page_size,
		query_mode,
		sort,
		spaceid,
		sql,
		systemid,
		tablename
	}];
	const data = await send_msg<Array<{
		tablename: string;
		dataArray: any[];
	}>>(action_id, session_id, DBACC_FIND, msg);
	if (data.length > 0) {
		for (const element of data) {
			if (element.tablename === tablename) {
				return element.dataArray;
			}
		}
	}
	return [];
}
